if orders and not orders.GetActiveStops then


function orders:GetActiveStops(sec_code, sort_field, filter)
	return self:GetAllStops(sec_code, sort_field,
			function(o)
				return	bit.band(o.flags, 35) == 1
						and (not filter or filter(o))
			end
		)
end


function orders:GetStops(sec_code, farest_first)
	return self:GetActiveStops(sec_code,
			function(list)
				if sec_code then
					local is_long = bit.test(list[1].flags, 2)
					table.sort(list,
						function(small, big)
							if is_long and not farest_first or not is_long and farest_first then
								return small.condition_price > big.condition_price
							else
								return small.condition_price < big.condition_price
							end
						end
					)
				end
			end,
			function(o)
				return	o.stop_order_type == 1
						and not orders:GetTakeLimitID(o.order_num)		-- stop
			end
		)
end


function orders:GetTakeStops(sec_code, farest_first)
	return self:GetActiveStops(sec_code,
			function(list)
				if sec_code then
					local is_long = bit.test(list[1].flags, 2)
					table.sort(list,
						function(small, big)
							if is_long and not farest_first or not is_long and farest_first then
								return small.condition_price < big.condition_price
							else
								return small.condition_price > big.condition_price
							end
						end
					)
				end
			end,
			function(o)
				return	o.stop_order_type == 9					-- t&s
						or o.stop_order_type == 1
						and orders:GetTakeLimitID(o.order_num)	-- stop & take limit
			end
		)
end


function orders:GetActiveStopGroups(sec_code, farest_first)
	local stops = self:GetActiveStops(sec_code, "order_num")
	--local stops = self:GetActivatedStops(sec_code)	-- test
	local groups = {}
	local ret = {}
	local key
	local count = 0
	
	--message(lib:toline(stops))
	
	for _, stop in pairs(stops) do
		key = tostring(stop.condition_price) .. "_" .. tostring(stop.condition_price2)
		if not groups[key] then
			groups[key] = {
				sl = self:GetStopPrice(stop),
				tp = self:GetTakePrice(stop),
				qty = 0,
				is_long = bit.test(stop.flags, 2),	-- position side, not the order
			}
		end
		table.insert(groups[key], stop)
		groups[key].qty = groups[key].qty + stop.qty
		count = count + 1
	end
	
	for _, group in pairs(groups) do
		table.sort(group,
			function(small, big)
				return small.qty < big.qty
			end
		)
	
		table.insert(ret, group)
	end
	
	table.sort(ret,
		function(small, big)
			if small.is_long and not farest_first or not small.is_long and farest_first then
				if small.tp and big.tp then
					return small.tp < big.tp
				elseif not small.tp and not big.tp then
					return small.sl > big.sl
				elseif small.tp and not big.tp then
					return true
				end
			else
				if small.tp and big.tp then
					return small.tp > big.tp
				elseif not small.tp and not big.tp then
					return small.sl < big.sl
				elseif not small.tp and big.tp then
					return true
				end
			end
		end
	)
	
	ret.count = count
	
	return ret, stops
end


end -- of file
